package com.self.campussecondhandmarket.controller.user;

import com.alibaba.fastjson2.JSON;
import com.self.campussecondhandmarket.entity.domain.Forum;
import com.self.campussecondhandmarket.entity.request.forum.AddReq;
import com.self.campussecondhandmarket.entity.request.forum.DetailId;
import com.self.campussecondhandmarket.entity.request.forum.ListByType;
import com.self.campussecondhandmarket.entity.response.forum.DetailResp;
import com.self.campussecondhandmarket.resp.Result;
import com.self.campussecondhandmarket.service.ForumService;
import lombok.NonNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author: Vincent
 * <p> DateTime: 2024/4/14 12:53
 * <p> Description: 论坛视图
 * <p>
 **/
@RestController
@RequestMapping("/api/forum")
@CrossOrigin(origins = "http://localhost:9999", allowCredentials = "true")
public class ForumController {
	
	@Autowired
	ForumService forumService;
	
	/**
	 * 发出论坛提问
	 */
	@PostMapping("/add")
	public Result list(@RequestBody @NonNull AddReq dto) {
		if (dto.getContent() == null
				|| dto.getType() == null
				|| dto.getImgs() == null) {
			return Result.error("参数异常");
		}
		forumService.add(dto);
		return Result.success("已提交");
	}
	
	@PostMapping("/category")
	public Result a() {
		return forumService.groupForum();
	}
	
	
	@PostMapping("/getListByType")
	public Result getListByType(@RequestBody @NonNull ListByType dto) {
		if (dto.getType() == null
				|| dto.getAction() == null) {
			return Result.error();
		}
		return forumService.getListByType(dto.getType());
	}
	
	@PostMapping("/detail")
	public Result detail(@RequestBody @NonNull DetailId id) {
		Forum forum = forumService.getById(id.getId());
		DetailResp vo = new DetailResp();
		vo.setContent(forum.getContent());
		List<String> strings = JSON.parseArray(forum.getImg(), String.class);
		vo.setImgs(strings);
		vo.setId(forum.getId());
		vo.setType(forum.getType());
		return Result.success(vo);
	}
	
	// ====================管理员接口
	@PostMapping("/admin/all")
	public Result all() {
		List<com.self.campussecondhandmarket.entity.response.forum.ListByType> vo = forumService.list().stream().map(o -> {
			com.self.campussecondhandmarket.entity.response.forum.ListByType list = new com.self.campussecondhandmarket.entity.response.forum.ListByType();
			BeanUtils.copyProperties(o, list);
			list.setImgs(JSON.parseArray(o.getImg(), String.class));
			return list;
		}).toList();
		return Result.success(vo);
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@PostMapping("/admin/delete/{id}")
	public Result delete(@PathVariable("id") Integer id) {
		forumService.removeById(id);
		return Result.success("已删除");
	}
	
	@PostMapping("/admin/saveOrUpdate")
	public Result saveOrUpdate(@RequestBody @NonNull Forum forum) {
		forum.setImg("[\""+forum.getImg()+"\"]");
		forumService.saveOrUpdate(forum);
		return Result.success("已保存");
	}
	
	@PostMapping("/admin/filter")
	public Result filter(@RequestBody @NonNull ListByType dto) {
		return forumService.getListByType(dto.getType());
	}
}
